﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.Util;

namespace luceneindex
{
    class Program
    {
		static void Generate()
		{
			Console.Title = "Generate Index";
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("******************************************************************");
            Console.WriteLine("        Please select which operation you want to select          ");
            Console.WriteLine("            if you input 1,will generate Full index      ");
            Console.WriteLine("            if you input 2,will generate Inc index      ");
            Console.WriteLine("            if you input 3,will show the record count in the index");
            Console.WriteLine("******************************************************************\n");
            Console.ResetColor();
            luceneWrapper library = new luceneWrapper
            {
                IndexFoldString = ConfigurationManager.AppSettings["indexPath"],
                LogFilePath = ConfigurationManager.AppSettings["logPath"],
                DbConnString = ConfigurationManager.AppSettings["dbParam"],
                packageName = ConfigurationManager.AppSettings["packageName"],
                updateSPName = ConfigurationManager.AppSettings["UpdateSProcName"]
            };

            try
            {
                string str = Console.ReadLine();
                switch (str)
                {
                    case "1":
                        Console.WriteLine("\nyou input is 1,so it will generate full index,please waiting...");
                        library.FullIndexing();
                        break;
                    case "2":
                        Console.WriteLine("\nyou input is 2,so it will generate Inc index,please waiting...");
                        if (!File.Exists(library.IndexFoldString + @"\segments.gen"))
                        {
                            throw new Exception("it should generate full index for there are no index file in there...");
                        }
                        library.IncIndexing();
                        break;
                    case "3":
                        Console.WriteLine("\nyou input is 3,so it will show how many records in index...");
                        IndexSearcher searcher = new IndexSearcher(new SimpleFSDirectory(new DirectoryInfo(ConfigurationManager.AppSettings["indexPath"])), true);
                        Console.WriteLine(string.Format("\nnow there are {0} index in the index file", searcher.MaxDoc));
                        break;
                    case "bb":
                        Console.WriteLine("please input term key:");
                        string fld = Console.ReadLine();
                        Console.WriteLine("the key u input is:" + fld);
                        Console.WriteLine("please input term value:");
                        string txt = Console.ReadLine();
                        Console.WriteLine("the value u input is:" + txt);
                        Term term = new Term(fld, txt);
                        library.DeleteByTerm(term);
                        break;
                    default:
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\nplease input 1 or 2 or 3,others is no useless...");
                        Console.ResetColor();
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nsorry,there are some error happen,error detail as belows:");
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }
		}
		
		static void Search()
		{
			string indexPath = @"E:\Boundless\IndexFile";
            var indexSearch = new IndexSearcher(new Lucene.Net.Store.SimpleFSDirectory(new System.IO.DirectoryInfo(indexPath)));
            Term term = new Term("CNTY_NAME", "canada");
            var query = new TermQuery(term);
            //var queryFilter = new QueryFilter(new TermQuery(term));
            //BitArray array = queryFilter.Bits(indexSearch.IndexReader);
            //DocIdSet docIdSet = queryFilter.GetDocIdSet(indexSearch.IndexReader);
            
            var queryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
            DocIdSet docIdSet = queryFilter.GetDocIdSet(indexSearch.IndexReader);
            DocIdSetIterator docIdSetIterator = docIdSet.Iterator();
            var temp = new OpenBitSetDISI(docIdSetIterator, 2500);
            var total = temp.Cardinality();
            while (docIdSetIterator.NextDoc() != DocIdSetIterator.NO_MORE_DOCS)
            {
                int docId = docIdSetIterator.DocID();
            }

            int docNum = indexSearch.MaxDoc;
            for (int i = docNum - 1; i >= 0; i--)
            {
                Document doc = indexSearch.Doc(i);
                var queryf = doc.fields_ForNUnit;
                foreach (var tmp in queryf)
                {

                }
            }

            TopDocs topDocs = indexSearch.Search(query, 10);
            ScoreDoc[] docs = topDocs.ScoreDocs;
            for (int i = docs.Count() - 1; i >= 0; i--)
            {
                Document doc = indexSearch.Doc(docs[i].Doc);
                var queryf = doc.fields_ForNUnit;
                foreach (var tmp in queryf)
                {

                }
            }
		}
		
        static void Main(string[] args)
        {
            //Generate();
			
			//Search();

            Console.WriteLine("\nplease press the enter key to exit...");
            Console.ReadLine();
        }
    }
}
